package org.ubc.cs410.disposal2020.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ubc.cs410.disposal2020.Game;
import org.ubc.cs410.disposal2020.GameFactory;
import org.ubc.cs410.disposal2020.NoActiveGameException;
import org.ubc.cs410.disposal2020.PMF;
import org.ubc.cs410.disposal2020.Player;

import com.google.appengine.api.users.UserServiceFactory;

public class GameServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {

		Game game = null;

		// See if user already has a active game.
		try {
			game = GameFactory.getCurrentGame();
		} catch (NoActiveGameException e) {
			game = GameFactory.getGameBySlug(req.getPathInfo().substring(1));
			if (game == null) {
				resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No such game."); //Send 404
				return;
			}
			if (game.isOver()) {
				// Template data
				Map<String, Object> root = new HashMap<String, Object>();

				// Page title
				root.put("title", "Game Review");

				int score = -1;
				for (Player p : game.getPlayers()) {
					if (p.getUser().equals(UserServiceFactory.getUserService().getCurrentUser().getEmail())) {
						root.put("yourScore", new Integer(p.getScore()));
						if (score==-1) {
							score = p.getScore();
						} else {
							if (p.getScore()>score) {
								root.put("winner", "YOU have won");
							} else if (p.getScore()<score) {
								root.put("winner", "Your opponent ("+p.getUser()+") has won");
							} else {
								root.put("winner", "The game ended with a tie");
							}
						}
					} else {
						root.put("opEmail", p.getUser());
						root.put("opScore", new Integer(p.getScore()));
						if (score==-1) {
							score = p.getScore();
						} else {
							if (p.getScore()<score) {
								root.put("winner", "YOU have won");
							} else if (p.getScore()>score) {
								root.put("winner", "Your opponent ("+p.getUser()+") has won");
							} else {
								root.put("winner", "The game ended with a tie");
							}
						}
					}
				}

				// Do the rendering
				ServletUtils.renderTemplate(getServletContext(), root, "PostGame.ftl",
						resp, req.getRequestURI());
				return;
			}
		}	  

		// Template data
		Map<String, Object> root = new HashMap<String, Object>();

		// Page title
		root.put("title", "Game in Session");
		root.put("id", new Long(game.getId()).toString());
		root.put("mapId", new Long(game.getMapId()).toString());
		root.put("userEmail", UserServiceFactory.getUserService().getCurrentUser().getEmail());

		for (Player p : game.getPlayers()) {
			if (!p.getUser().equals(UserServiceFactory.getUserService().getCurrentUser().getEmail())) {
				root.put("opEmail", p.getUser());
			}
		}

		// Do the rendering
		ServletUtils.renderTemplate(getServletContext(), root, "Game.ftl",
				resp, req.getRequestURI());
	}
}
